package com.ibm.cloud.banking.ldc;

import com.alipay.sofa.rpc.api.ldc.LdcRouteJudgeResult;
import com.alipay.sofa.rpc.api.ldc.LdcRouteProvider;
import com.alipay.sofa.rpc.common.utils.CommonUtils;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.zoneclient.api.EnterpriseZoneClientHolder;
import com.ibm.cloud.banking.platform.message.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 单元化规则
 * @author cmk
 * @date 2022/8/5
 */
public class PocLdcRouterProvider implements LdcRouteProvider {
    public static final Logger logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    @Override
    public LdcRouteJudgeResult uidGenerator(ConsumerConfig consumerConfig, SofaRequest sofaRequest) {
//        logger.info("-------------单元化判断--------------------");
        LdcRouteJudgeResult result = new LdcRouteJudgeResult();
        //如果没有开启ldc，那么就直接返回false
        if (!EnterpriseZoneClientHolder.isZoneMode()) {
            return result;
        }

        Object[] methodArgs = sofaRequest.getMethodArgs();
        if (CommonUtils.isEmpty(methodArgs)) {
            result.setSuccess(false);
            return result;
        }
        Object methodArg = methodArgs[0];
        if (methodArg instanceof Request){
            Request request = (Request) methodArg;
            result.setSuccess(true);
            String uid=request.getReqHeader().getReqGlbSerNo().substring(0,2);
            logger.info("uid-----"+uid);
            result.setRouteId(uid);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    @Override
    public int order() {
        return 999;
    }
}
